package org.ziky.common.framework.wicket.markup.html.panel;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.IMarkupFragment;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
import org.apache.wicket.markup.html.panel.AbstractMarkupSourcingStrategy;

public class FwDefaultMarkupSourcingStrategy extends AbstractMarkupSourcingStrategy {

    private static FwDefaultMarkupSourcingStrategy instance = new FwDefaultMarkupSourcingStrategy();

    public static final FwDefaultMarkupSourcingStrategy get() {
        return instance;
    }

    public void onComponentTag(Component component, ComponentTag tag) {
    }

    public void onComponentTagBody(Component component, MarkupStream markupStream, ComponentTag openTag) {
        component.onComponentTagBody(markupStream, openTag);
    }

    public IMarkupFragment getMarkup(MarkupContainer container, Component child) {
        IMarkupFragment markup = container.getMarkup();
        if (markup == null) {
            return null;
        }

        if (child == null) {
            return markup;
        }

        markup = markup.find(child.getId());
        if (markup != null) {
            return markup;
        }

        markup = searchMarkupInTransparentResolvers(container, child);
        if (markup != null) {
            return markup;
        }

        if (Character.isDigit(child.getId().charAt(0))) {
            String id = child.getId();
            boolean miss = false;
            for (int i = 1; i < id.length(); ++i) {
                if (Character.isDigit(id.charAt(i)))
                    continue;
                miss = true;
                break;
            }

            if (!(miss)) {
                markup = container.getMarkup();
            }

        }

        return markup;
    }

    public void renderHead(Component component, HtmlHeaderContainer container) {
    }

}
